package com.kordar.admin.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kordar.admin.enums.CommonActive;
import com.kordar.admin.enums.EnumUtils;
import com.kordar.admin.kernel.util.RequestHolder;
import com.kordar.admin.kernel.wrapper.BatchDeleteWrapperEntity;
import com.kordar.admin.kernel.wrapper.UpdateWrapperEntity;
import com.kordar.admin.modular.sys.entity.TenantEntity;
import com.kordar.admin.modular.sys.service.DatasourceService;
import com.kordar.admin.modular.sys.service.TenantService;
import com.kordar.datalist.DatalistService;
import com.kordar.datalist.ResultEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@AllArgsConstructor
@Controller
@RequestMapping("sys")
public class TenantController {

    private final TenantService tenantService;
    private final DatalistService datalistService;
    private final DatasourceService datasourceService;

    @ModelAttribute("activeLabels")
    public Map<Integer, String> active() {
        return EnumUtils.EnumToMapInt(CommonActive.class);
    }

    @ModelAttribute("datasourceLabels")
    public Map<String, String> datasourceLabels() {
        return datasourceService.labels();
    }

    @GetMapping("tenant/index")
    public String index(Model model) {
        model.addAttribute("data", datalistService.render(tenantService.list(), TenantEntity.class));
        return "pages/modular/system/tenant/index";
    }

    @GetMapping("tenant/form")
    public String form(Model model, @RequestParam(required = false, defaultValue = "") String code) {
        if (code.equals("")) {
            model.addAttribute("header", "新建租户");
            model.addAttribute("tenant", new TenantEntity());
        } else {
            model.addAttribute("header", "修改租户");
            model.addAttribute("isUpdate", true);
            model.addAttribute("tenant", tenantService.getById(code));
        }

        return "pages/modular/system/tenant/form";
    }

    @PostMapping("tenant/save")
    @ResponseBody
    public ResultEntity<Object> save(@Validated TenantEntity tenantEntity) {
        tenantService.saveOrUpdate(tenantEntity);
        return new ResultEntity<>();
    }

    @RequestMapping("tenant/edit")
    @ResponseBody
    public ResultEntity<Object> edit(@RequestBody UpdateWrapperEntity<TenantEntity> entity) {
        UpdateWrapper<TenantEntity> query = entity.where(TenantEntity.class).data().getQuery();
        tenantService.update(query);
        return new ResultEntity<>();
    }

    @PostMapping("tenant/delete")
    @ResponseBody
    public ResultEntity<Object> del(String code) {
        tenantService.removeById(code);
        return new ResultEntity<>();
    }

    @RequestMapping("tenant/batch-delete")
    @ResponseBody
    public ResultEntity<Object> del(@RequestBody BatchDeleteWrapperEntity<String> entity) {
        Set<String> ids = entity.getIds();
        tenantService.removeByIds(ids);
        return new ResultEntity<>();
    }

    @RequestMapping("tenant/init")
    @ResponseBody
    public ResultEntity<Object> tenantInit(String code) {
        TenantEntity entity = tenantService.findOneByCode(code);
        if (entity == null) {
            return ResultEntity.error("初始化失败");
        }
        RequestHolder.getRequest().setAttribute("datasource", entity.getDatasource());
        if (tenantService.initSystem(entity)) {
            tenantService.updateTenant(code, 1);
            return ResultEntity.success();
        }
        return ResultEntity.error("初始化失败");
    }

    @RequestMapping("tenant/destroy")
    @ResponseBody
    public ResultEntity<Object> tenantDestroy(String code) {
        TenantEntity entity = tenantService.findOneByCode(code);
        if (entity == null) {
            return ResultEntity.error("销毁失败");
        }
        RequestHolder.getRequest().setAttribute("datasource", entity.getDatasource());
        if (tenantService.destroySystem(entity)) {
            tenantService.updateTenant(code, 0);
            return ResultEntity.success();
        }
        return ResultEntity.error("销毁失败");
    }

}
